import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:multi_image_picker/multi_image_picker.dart';
import 'package:wanchong/utils/upload.dart';
import 'package:wanchong/widget/app_bar.dart';
import 'package:wanchong/widget/image_picked.dart';

class FeedBack extends StatefulWidget {
  @override
  _FeedBackState createState() => _FeedBackState();
}

class _FeedBackState extends State<FeedBack> {
  //图片上传任务列表
  // List<StorageUploadTask> _upLoadTask = [];
  TextEditingController _contentController = TextEditingController();
  TextEditingController _phoneController = TextEditingController();
  // File _img;
  List<String> _imgs = [];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        child: Stack(
          children: <Widget>[
            Center(
              child: Text(
                "意见反馈",
                style: TextStyle(
                    color: Color(0xFF3C4450),
                    fontWeight: FontWeight.w700,
                    fontSize: 18.0),
              ),
            ),
            GestureDetector(
              onTap: () {
                // print("object");
                Navigator.of(context).pop();
              },
              child: Container(
                padding: EdgeInsets.only(right: 15.0),
                color: Colors.transparent,
                height: double.infinity,
                child: Image.asset("assets/login/top_return_icon.png"),
              ),
            ),
            GestureDetector(
              onTap: () {
                print("object");
                // Navigator.of(context).pop();
              },
              child: Align(
                alignment: Alignment.centerRight,
                child: Container(
                  width: 60.0,
                  color: Colors.transparent,
                  height: double.infinity,
                  // padding: EdgeInsets.only(right: 15.0),
                  child: Center(
                    child: Text(
                      "提交",
                      style:
                          TextStyle(color: Color(0xFF3C4450), fontSize: 16.0),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
      body: Container(
        // padding: ,
        child: Column(
          children: <Widget>[
            SizedBox(
              height: 15.0,
            ),
            Container(
              color: Colors.white,
              padding: EdgeInsets.all(15.0),
              child: _editor(),
            ),
            SizedBox(
              height: 15.0,
            ),
            Container(
              padding: EdgeInsets.only(left: 15.0, right: 15.0),
              color: Colors.white,
              child: TextField(
                controller: _phoneController,
                decoration: InputDecoration(
                  border: InputBorder.none,
                ),
                style: TextStyle(color: Color(0xFF424D59), fontSize: 14.0),
                keyboardType: TextInputType.number,
                maxLength: 11,
              ),
            ),
            SizedBox(
              height: 15.0,
            ),
            Text(
              "您的联系方式有助于我们沟通和解决问题，信息将会加密传输，仅工作人员可见",
              style: TextStyle(color: Color(0xFFB4BEC6), fontSize: 12.0),
            )
          ],
        ),
      ),
    );
  }

  Widget _editor() {
    return Column(
      children: <Widget>[
        TextField(
          maxLines: 5,
          controller: _contentController,

          style: TextStyle(color: Color(0xFF424D59), fontSize: 14.0),
          decoration: InputDecoration(
            border: InputBorder.none,
            hintText: "请描述您遇到的问题，您提供的有效截图有助于我们沟通和解决问题",
            hintStyle: TextStyle(
              fontSize: 14.0,
              color: Color(0xFFB4BEC6),
            ),
          ),
          // maxLengthEnforced = true,
          maxLength: 150,
        ),
        SizedBox(
          height: 15.0,
        ),
        Divider(
          color: Color(0xFFDBDBDB),
          height: 1.0,
        ),
        SizedBox(
          height: 15.0,
        ),
        _imgs.length == 0
            ? GestureDetector(
                onTap: () => _pickImage(),
                child: Container(
                  alignment: Alignment.centerLeft,
                  // width: 64.0,
                  height: 64.0,
                  child: Image.asset("assets/setting/set_add_img.png"),
                ),
              )
            : _buildImagePicked(),
      ],
    );
  }

  Widget _buildImagePicked() {
    return Wrap(
      spacing: 8.0,
      runSpacing: 8.0,
      // alignment: WrapAlignment.start,
      children: List.generate((_imgs.length + 1), (index) {
        if (index != _imgs.length) {
          return ImagePicked(
            imageUrl: _imgs[index],
            index: index,
            valueChanged: (index) {
              setState(() {
                _imgs.removeAt(index);
              });
            },
          );
        } else {
          return GestureDetector(
            onTap: () => _pickImage(),
            child: Container(
              alignment: Alignment.centerLeft,
              width: 64.0,
              height: 64.0,
              child: Image.asset("assets/setting/set_add_img.png"),
            ),
          );
        }
      }),
    );
  }

  _pickImage() async {
    var requestList =
        await MultiImagePicker.pickImages(maxImages: 3, enableCamera: false);
    // for (int i=0;i<requestList.length;i++) {
    //   var tmp = requestList[i];
    //   tmp.
    // }
    //如果选取的照片列表大于0
    if (requestList.length != 0) {
      // final String uuid = Uuid().v1();
      //请求原始图片数据
      for (int i = 0; i < requestList.length; i++) {
        FileUtils.getInstance().upload(
            requestList[i],
            (data) {
              Map<String, dynamic> dataMap = json.decode(data);
              if (dataMap["code"] == 200) {
                _imgs.add(dataMap["data"]);
                setState(() {});
              }
              print(data);
            },
            () {},
            (r, t) {
              print((r / t * 100).toStringAsFixed(0) + "%");
            });
      }
    }
    print(requestList);
  }
}
